<template>
  <div class="cart-sku">
    <div ref="target" class="attrs">
      <span class="ellipsis">{{ props.attrsText }}</span>
      <i class="iconfont icon-angle-down" @click="toggle"></i>
      <div class="layer" v-if="visible">
        <div class="loading" v-if="!goods"></div>
        <GoodsSku @change-sku="changeSkuHandler" v-if="goods" :skuId="props.skuId" :goods="goods"></GoodsSku>
        <xtx-button @click="submitSku" v-if="goods" type="primary" size="mini" style="margin-left:60px">确认</xtx-button>
      </div>
    </div>
  </div>
</template>
<script setup>
import { defineProps, ref, defineEmits } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getSpecsAndSkus } from '@/api/cart'
// 导入商品详情页中的goods-sku组件
import GoodsSku from '@/views/product/components/goods-sku.vue'
const props = defineProps({
  attrsText: {
    type: String,
    default: ''
  },
  skuId: {
    type: String,
    default: ''
  }
})
// 父组件的自定义事件
const emit = defineEmits(['change'])

// 控制sku区域的显示和隐藏
const visible = ref(false)
// 关闭sku区域
function hide() {
  visible.value = false
  // 关闭时数据置空
  goods.value = null
}
// 显示sku区域
function show() {
  visible.value = true
  // 发送数据请求sku数据
  getSkuAndSpecs(props.skuId)
}
// 显示和隐藏切换
function toggle() {
  visible.value ? hide() : show()
}
// 鼠标点击attrs这个dom外部时隐藏sku区域
const target = ref(null)
onClickOutside(target, () => {
  hide()
})
// 获取当前商品默认选中的sku数据
// 定义变量保存当前商品的sku数据
const goods = ref(null)
async function getSkuAndSpecs(skuId) {
  const { result } = await getSpecsAndSkus(skuId)
  goods.value = result
  console.log(result)
}

// 接收组件修改后的sku数据
const currSku = ref(null)
function changeSkuHandler(sku) {
  currSku.value = sku
}
// 提交接收到sku数据到父组件中
function submitSku() {
  // 用户有可能直接点击确定，此时currSku值为null,如果用户选择还是我们默认，那么没必要重新修改
  if (currSku.value !== null && currSku.value.skuId && currSku.value.skuId !== props.skuId) {
    emit('change', currSku.value)
  }
  // 关闭组件
  hide()
}
</script>
<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display: inline-block;

  .attrs {
    line-height: 24px;
    display: flex;

    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }

    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }

  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor, 50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;

    &::before {
      content: "";
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(.8, 1) rotate(45deg);
    }

    .loading {
      height: 224px;
      background: url(@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>